package com.gxa.shop.servlet.order; /**
 * ${PACKAGE_NAME}
 * User: coma
 * Description:
 * Version: V1.0
 */

import com.alibaba.fastjson.JSON;
import com.gxa.shop.bean.pojo.Orders;
import com.gxa.shop.bean.vo.OrderMyVo;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.service.order.OrderService;
import com.gxa.shop.service.order.impl.OrderServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;

@WebServlet(name = "OrderControllerServlet", value = "/member/order/myOrder")
public class OrderControllerServlet extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();

        // 获取并验证参数
        String userIdParam = request.getParameter("userId");
        String statusParam = request.getParameter("orderState");
        String pageParam = request.getParameter("page");
        String pageSizeParam = request.getParameter("pageSize");


        if (userIdParam == null || statusParam == null ) {
            writer.write(JSON.toJSONString(R.error("缺少必要参数")));
            return;
        }


            Integer userId = Integer.parseInt(userIdParam);
            Integer status = Integer.parseInt(statusParam);
             Integer page = Integer.parseInt(pageParam);
             Integer pageSize = Integer.parseInt(pageSizeParam);


            // 调用服务层方法
            Integer total = orderService.totalOrder(userId, status);
            List<OrderMyVo> list = orderService.findMyOrder(userId, status, page, pageSize);

            // 使用 R 构建响应对象，并直接添加数据到 data 中
            R<Object> r = R.ok()
                    .addData("total", total)
                    .addData("orders", list != null ? list : Collections.emptyList());


            String  result= JSON.toJSONString(r);
            writer.write(result);
            writer.flush();
            writer.close();


    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
